package com.example.interviewrecorder.view

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import android.widget.Toast
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.interviewrecorder.LyyBaseActivity
import com.example.interviewrecorder.LyyDatabase
import com.example.interviewrecorder.R
import com.example.interviewrecorder.dao.LyyUserDao
import com.example.interviewrecorder.databinding.ActivityRegisterBinding
import com.example.interviewrecorder.model.LyyUser
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class RegisterActivity : LyyBaseActivity(), View.OnClickListener {
    private lateinit var binding: ActivityRegisterBinding
    private val lyyUserDao: LyyUserDao = LyyDatabase.getDatabase().getLyyUserDao()
    private val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                EXISIT_MSG -> Toast.makeText(
                    this@RegisterActivity,
                    getString(R.string.user_exist_tip),
                    Toast.LENGTH_SHORT
                ).show()

                SUCCESS_MSG -> Toast.makeText(
                    this@RegisterActivity,
                    getString(R.string.register_success_tip),
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

    companion object {
        const val EXISIT_MSG = 1
        const val SUCCESS_MSG = 2
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initEvents()
    }

    private fun initEvents() {
        binding.registerBtn.setOnClickListener(this)
        binding.loginTv.setOnClickListener(this)
        binding.findPsdTv.setOnClickListener(this)
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.registerBtn -> register()
            R.id.loginTv -> startActivity(Intent(this, LoginActivity::class.java))
            R.id.findPsdTv -> FindPasswordActivity.start()
        }
    }

    fun register() {
        val userName = binding.userNameEt.text.toString().trim()
        val password1 = binding.passwordEt1.text.toString().trim()
        val password2 = binding.passwordEt2.text.toString().trim()
        if (password1 != password2) {
            Toast.makeText(this, getString(R.string.password_not_match_tip), Toast.LENGTH_SHORT)
                .show()
            return
        }
        val lyyUser = LyyUser(
            userName,
            password1,
            "",
            getString(R.string.male),
            0,
            "",
            System.currentTimeMillis()
        )
        lifecycleScope.launch {
            launch(Dispatchers.IO) {
                repeatOnLifecycle(Lifecycle.State.STARTED) {
                    val list = lyyUserDao.queryAllUser()
                    var isExist = false//标识用户名是否已经存在
                    for (user in list) {
                        val uName = user.userName
                        if (uName == userName)
                            isExist = true
                        break
                    }
                    if (isExist) {
                        val msg = Message.obtain()
                        msg.what = EXISIT_MSG
                        handler.sendMessage(msg)
                    } else {
                        lyyUserDao.insertLyyUser(lyyUser)
                        val msg = Message.obtain()
                        msg.what = SUCCESS_MSG
                        handler.sendMessage(msg)
                    }
                }
            }
        }
    }
}